// import * as fs from 'fs';
// import * as FormData from 'form-data';
// import * as ProgressBar from 'progress';
// import * as axios from 'axios';
// import * as moment from 'moment'

const fs = require('fs');
const Path = require('path');
const FormData = require('form-data');
const ProgressBar = require('progress');
const axios = require('axios');
const { Blob } = require('buffer');
const concat = require('concat-stream');

const mime = require('mime');
const { async } = require('rxjs');

async function thirdUpload(action = 'GetAliDM', filePath) {
  const url = `http://head.jcor.cn/api/Avatar/${action}`;

  const formData = new FormData();
  const fileName = Path.basename(filePath);
  const Authorization =
    'SHA256 eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJvcGVuaWQiOiJvQUoyNTZRaWdXbzZTTHhrMjJQSG5vN05sazlFIiwiYXBwX29wZW5pZCI6Im9BSjI1NlFpZ1dvNlNMeGsyMlBIbm83TmxrOUUiLCJhcHBfY29kZSI6IkhFQUQiLCJpZCI6MzUyODUzLCJuaWNrbmFtZSI6IuWwj-Wwj-aItOWFiOeUnyIsInVzZXJfcGhvbmUiOm51bGwsImhlYWRpbWd1cmwiOiJodHRwczovL3RoaXJkd3gucWxvZ28uY24vbW1vcGVuL3ZpXzMyLzUwekFMTVZSSGdzZzZRVWliaWFUemliSHFtcE1ZSFMwT3pWcVRsWXJkQkxmanhvTWljSE43UnVVMk1IVDVqdzhnVUpvaFVuTWR3MTl2ekt2RTNaNFdWcEhody8xMzIiLCJzZXgiOjAsImF1ZGlvX3RpbWUiOjE4MDAsImV4cCI6MTcxNjgxOTYzNS40MDUyNTkxfQ.E7lQStfy_UpnbcB8iemVqCmSOVygmqNduzHm_Q3KQto';

  formData.append('coverFile', fs.createReadStream(filePath));
  console.log(filePath);
  const headers = formData.getHeaders();
  const config = {
    ...headers,
    'Content-Type':
      'multipart/form-data;boundary=----WebKitFormBoundaryVCFSAonTuDbVCoAN;image/*',
    Authorization: Authorization,
  };
  // formData.getLength(async function (err, length) {
  //   if (err) {
  //     return;
  //   }
  //   //设置长度，important!!!
  //   config['Content-length'] = length;
  // });

  return new Promise(resole=>{
    formData.pipe(
      concat({ encoding: 'buffer' }, async (data) => {
        const response = await axios.default.post(url, data, {
          headers: config,
        });
        try {
          console.log(`Upload complete:`);
          console.log(response.data);
          resole(response.data.Data)
        } catch (error) {
          resole(null)
        }
      }),
    );
  })

}
async function getData() {
  const imgData = await thirdUpload(
    'GetAliSM',
    'H:/1111pro/rose-api/image/test.png',
  );

  console.log(imgData)
}

getData();
